package argo;

import java.util.HashMap;
import java.util.Map;

class NoActiveTransactionException extends Exception {
    public NoActiveTransactionException(String msg) {
        super(msg);
    }
}

class DatabaseSimulator {
    // YOUR SOLUTION HERE
    private boolean transactionActiveTag = false;
    private final Map<String, String> permanentlyStorageData = new HashMap<>();
    private Map<String, String> transactionBufferData = null;

    public DatabaseSimulator() {
    }

    public void begin() throws Exception {
        //check active transaction
        if (!transactionActiveTag) {
            transactionBufferData = new HashMap<>();
            transactionActiveTag = true;
        } else {
            throw new Exception("Transaction Processing, Waiting Please.");
        }
    }

    public String get(String key) throws Exception {
        if (transactionBufferData != null && transactionBufferData.containsKey(key)) {
            return transactionBufferData.get(key);
        }
        return permanentlyStorageData.get(key);
    }

    public void set(String key, String value) throws Exception {
        if (!transactionActiveTag) {
            throw new NoActiveTransactionException("No Active Transaction Exception.");
        }
        transactionBufferData.put(key, value);
    }

    public Integer count() throws Exception {
        return permanentlyStorageData.size();
    }

    public void commit() throws Exception {
        if (!transactionActiveTag) {
            throw new NoActiveTransactionException("No Active Transaction Exception.");
        }
        //persist data and clean buffer and tag
        permanentlyStorageData.putAll(transactionBufferData);
        transactionBufferData = null;
        transactionActiveTag = false;

    }

    public void rollback() throws Exception {
        if (!transactionActiveTag) {
            throw new NoActiveTransactionException("No Active Transaction Exception.");
        }
        transactionBufferData = null;
        transactionActiveTag = false;
    }
}

public class NumoraTest {

    public int solution(String S) {
        // Implement your solution here
        int moveTimes = 0;
        char lastChar = ' ';
        int sameCharCount = 1;

        for (int i = 0; i < S.length(); i++) {
            //System.out.println("i: "+i+ ", moveTimes: "+moveTimes);
            char currentChar = S.charAt(i);

            if (currentChar == lastChar) {
                sameCharCount++;
                //processing triplet char
                if (sameCharCount >= 2 && (i + 1 < S.length() && S.charAt(i + 1) == currentChar)) {
                    moveTimes++;
                    //skip this triplet char
                    if (i + 2 < S.length()) {
                        //i=i+3, i++ will process in the end of this loop
                        i = i + 2;
                        lastChar = S.charAt(i);
                        sameCharCount = 1;
                    }
                }
            } else {
                //if not same, record current char for next loop
                sameCharCount = 1;
                lastChar = currentChar;
            }
        }


        return moveTimes;
    }


    public int solution1(int[] A, int X, int Y, int Z) {


        int maxWaitTime = 0;
        int[] OilRemain = {X, Y, Z};
        boolean[] busyTag = {false, false, false};
        int[] waitTimeList = {0, 0, 0};

        for (int i = 0; i < A.length; i++) {
            int demandOil = A[i];
            boolean found = false;

            // find Iol
            for (int j = 0; j < 3; j++) {
                if (!busyTag[j] && OilRemain[j] >= demandOil) {
                    found = true;
                    busyTag[j] = true;
                    // log current wait time
                    waitTimeList[j] = demandOil;
                    if (waitTimeList[j] > maxWaitTime) {
                        maxWaitTime = waitTimeList[j];
                    }
                    //update oil remain
                    OilRemain[j] -= waitTimeList[j];
                    break;
                }
            }

            // if not find oil, waiting process for oil tanker
            if (!found) {
                for (int j = 0; j < 3; j++) {
                    if (demandOil > OilRemain[j]) {
                        return -1;
                    } else {
                    }
                }
            }
            //update tag
            for (int j = 0; j < 3; j++) {
                if (busyTag[j]) {
                    busyTag[j] = false;
                }
            }
        }

        return maxWaitTime;
    }


    public static void main(String[] args) throws Exception {
      /*  System.out.println(new NumoraTest().solution("baaabbaabbba"));
        System.out.println(new NumoraTest().solution("baaaaab"));
        System.out.println(new NumoraTest().solution("baabab"));
*/

        NumoraTest solution = new NumoraTest();
        int[] A = {2, 8, 4, 3, 2};
        int X = 7, Y = 11, Z = 3;
        System.out.println(solution.solution1(A, X, Y, Z)); // 应该输出8


    }
}
